package books4africa

class DonationException extends RuntimeException{
	String message
	String donation
}

class DonationService {

	def inventoryService
    boolean transactional = true
	
	def newDonation(Books book, User user, params){
		
		def donation = new Donation([bookQty: params.bookQty, donationDate:new Date(), book:book, donor:user ])
		
		if (!donation.save(flush:true)){
			throw new DonationException(
				message:"Unable to finish registering this donation",
				donation: donation.errors.each{println it}	
			)
		}
		
		inventoryService.addToInventory(donation, params.bookQty.toInteger())
		
		return donation
	}
}
